var webpack = require('webpack')
var autoprefixer = require('autoprefixer')
var ExtractTextPlugin = require("extract-text-webpack-plugin")

// html-webpack-plugin插件，webpack中生成HTML的插件，
// 具体可以去这里查看https://www.npmjs.com/package/html-webpack-plugin
var HtmlWebpackPlugin = require('html-webpack-plugin')
var path = require("path")


var BUILD_ENV = process.env.BUILD_ENV

var PUBLIC_PATH = {
    'dev': '/',
    'test': 'http://test.yourhost.com/antd-admin/',
    'prod': 'http://myqianlan.com/antd-admin-boilerplate/dist/',
}

var webpackConfig = {

    entry: {
        index: './src/app.js',
        vendor: ['jquery', 'moment', 'react', 'react-dom', 'react-router', 'antd', 'react-redux', 'redux']
    },
    cache: true,
    output: {
        path: path.resolve(__dirname, './dist'),
        publicPath: '/',
        filename: '[name].[chunkHash:8].js',
        chunkFilename: "[name].[chunkHash:8].js",
    },
    module: {
        loaders: [
            {
                test: /\.(js|jsx)$/,
                exclude: [/node_modules/, /component_dev/],
                loader: 'babel-loader',
            }, {
                test: /\.css$/,
                loader: 'style-loader!css-loader',
                // loader: ExtractTextPlugin.extract(['css', 'postcss'])
            }, {
                test: /\.scss$/,
                loader: 'style-loader!css-loader!sass-loader',
                // loader: ExtractTextPlugin.extract(['css', 'postcss', 'sass'])
            }, {
                test: /\.less$/,
                loader: 'style-loader!css-loader!less-loader',
                // loader: ExtractTextPlugin.extract(['css', 'postcss', 'less'])
            }, {
                test: /\.(png|jpg|jpeg|gif)$/i,
                loader: 'url-loader?name=[hash:8].[ext]&limit=8192',
            }, {
                //html模板加载器，可以处理引用的静态资源，默认配置参数attrs=img:src，处理图片的src引用的资源
                //比如你配置，attrs=img:src img:data-src就可以一并处理data-src引用的资源了，就像下面这样
                test: /\.html$/,
                loader: "html?attrs=img:src img:data-src"
            }, {
                //文件加载器，处理文件静态资源
                test: /\.(woff|woff2|ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                loader: 'file-loader?name=./fonts/[name].[ext]'
            }, {
                test: /\.json$/,
                loader: 'json-loader'
            }

        ]
    },

    plugins: [
        new webpack.DefinePlugin({
            'process.env': {
                'NODE_ENV': JSON.stringify("production")
            }
        }),
        new webpack.NoEmitOnErrorsPlugin(),
        new webpack.ProvidePlugin({
            $: 'jquery',
            jQuery: 'jquery',
            'windows.jQuery': 'jquery',
        }),
        new webpack.optimize.CommonsChunkPlugin({
            names: ['vendor', 'manifest'],
            minChunks: Infinity
        }),
        new webpack.optimize.UglifyJsPlugin({
            compress: {
                warnings: false,
                drop_console: true
            }
        }),
        new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /zh-cn/),
        new HtmlWebpackPlugin({

            filename: 'index.html', //生成的html存放路径，相对于path
            template: './src/index.ejs', //html模板路径
            title: '凯贝姆',
            inject: true, //js插入的位置，true/'head'/'body'/false
            hash: true, //为静态资源生成hash值
            minify: { //压缩HTML文件
                removeComments: true, //移除HTML中的注释
                collapseWhitespace: false //删除空白符与换行符
            },
            chunksSortMode: 'dependency',
            externalsAssets: {}
        }),
        new ExtractTextPlugin({
            filename: 'app_[hash].css',
            disable: false,
            allChunks: true
        }),
    ]
}

module.exports = webpackConfig
